// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.query.sections;

import org.dengues.designer.ui.database.editors.DBFileEditorInput;
import org.dengues.designer.ui.query.AbstractFormPage;
import org.dengues.model.DenguesModelManager;
import org.dengues.model.database.DBColumn;
import org.dengues.model.database.DBTable;
import org.dengues.model.database.DatabaseDiagram;
import org.dengues.model.database.SectionGroups;
import org.dengues.model.database.SectionSorts;
import org.dengues.ui.query.QueryUiUtils;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.IFormPart;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2007-12-8 qiang.zhang $
 * 
 */
public class TableColumnsSection extends AbstractSectionPart {

    /**
     * Qiang.Zhang.Adolf@gmail.com ColumnsSection constructor comment.
     * 
     * @param parent
     * @param toolkit
     * @param style
     */
    public TableColumnsSection(AbstractFormPage page, Composite parent) {
        super(page, parent, Section.DESCRIPTION, new String[] { "Add Group", "Add Sort" });
        getSection().setText("Table Columns");
        getSection().setDescription("Specify group/sort from available columns..");
        getButton(BUTTON_ADD).setEnabled(false);
        getButton(BUTTON_EDIT).setEnabled(false);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.query.sections.TableSectionPart#buttonSelected(org.eclipse.swt.widgets.Button, int)
     */
    @Override
    protected void buttonSelected(Button button, int index) {
        Object o = ((IStructuredSelection) viewerPart.getSelection()).getFirstElement();
        if (o instanceof DBColumn) {
            DBColumn column = (DBColumn) o;
            if (BUTTON_ADD == index) {
                SectionGroups sectionGroups = DenguesModelManager.getDatabaseFactory().createSectionGroups();
                sectionGroups.setName(column.getTable().getName() + "." + column.getName());
                sectionGroups.setExpression(sectionGroups.getName());
                dbDiagram.getSectionGroups().add(sectionGroups);
                IFormPart[] parts = getManagedForm().getParts();
                for (IFormPart formPart : parts) {
                    if (formPart instanceof GroupsSection) {
                        GroupsSection section = (GroupsSection) formPart;
                        section.refreshViewerPart();
                    }
                }
            } else if (BUTTON_EDIT == index) {
                SectionSorts sectionSorts = DenguesModelManager.getDatabaseFactory().createSectionSorts();
                sectionSorts.setName(column.getTable().getName() + "." + column.getName());
                sectionSorts.setExpression(sectionSorts.getName());
                sectionSorts.setOrderBy(SortsSection.ORDERS.get(0));
                dbDiagram.getSectionSorts().add(sectionSorts);

                IFormPart[] parts = getManagedForm().getParts();
                for (IFormPart formPart : parts) {
                    if (formPart instanceof SortsSection) {
                        SortsSection section = (SortsSection) formPart;
                        section.refreshViewerPart();
                    }
                }
            }
            markDirty();
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.query.sections.AbstractSectionPart#createMainControl(org.eclipse.swt.widgets.Composite,
     * int, int, org.eclipse.ui.forms.widgets.FormToolkit)
     */
    @Override
    protected void createMainControl(Composite parent, int style, int span, FormToolkit toolkit) {
        DBFileEditorInput input = (DBFileEditorInput) dPage.getEditor().getEditorInput();
        DatabaseDiagram dbDiagram = input.getDbDiagram();
        viewerPart = QueryUiUtils.getInstance().createDbDiagramTree(parent, dbDiagram);
        viewerPart.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));
        viewerPart.addSelectionChangedListener(new ISelectionChangedListener() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
             */
            public void selectionChanged(SelectionChangedEvent event) {
                Object o = ((IStructuredSelection) event.getSelection()).getFirstElement();
                if (o instanceof DBColumn) {
                    getButton(BUTTON_ADD).setEnabled(true);
                    getButton(BUTTON_EDIT).setEnabled(true);
                } else if (o instanceof DBTable) {
                    getButton(BUTTON_ADD).setEnabled(false);
                    getButton(BUTTON_EDIT).setEnabled(false);
                }
            }
        });
    }

}
